#include "StdAfx.h"
#include "TransRealStream.h"
#include "TransMan.h"

TransRealStream::TransRealStream(TransMan *pTransMan)
{
	m_pTransMan = pTransMan;

	m_hRealStream  = NULL;
	m_hTransCamera = NULL;
}

TransRealStream::~TransRealStream(void)
{
	UnInitialize();
}


int TransRealStream::Initialize()
{

	return 0;
}

void TransRealStream::UnInitialize()
{
	StopRealStream();
}


int TransRealStream::StartRealStream( TG_SVR_RS_INFO *pRsInfo )
{
	int nRet = 0;

	m_xDevInfo     = pRsInfo->xDevInfo;
	m_hTransCamera = pRsInfo->hCamera;

// 	stESInfo *pESInfo = NULL;
// 	m_pTransMan->GetESInfo( &pRsInfo->xDevInfo,&pESInfo );
// 	if( NULL == pESInfo )
// 	{
// 		nRet =  TG_ERR_IPMISMATCH;
// 		return nRet;
// 	}

	if( NULL == m_hRealStream )
	{
		TG_REALSTREAM_INFO xStreamInfo;
		xStreamInfo.funCallBack = RealDataCallBack;
		xStreamInfo.nContext    = 0;
		xStreamInfo.pContext    = this;

		xStreamInfo.xConnectInfo.xUserInfo     = pRsInfo->xUserInfo;
		xStreamInfo.xConnectInfo.xDevInfo      = pRsInfo->xDevInfo;
		sprintf( xStreamInfo.xConnectInfo.szServerIP,m_xDevInfo.szESIP);
		xStreamInfo.xConnectInfo.nServerPort   = m_xDevInfo.nESPort;
		xStreamInfo.xConnectInfo.nLinkType     = 0;
		xStreamInfo.xConnectInfo.nNetBWState   = Net_BW_State_1;
		xStreamInfo.xConnectInfo.nLinkProtocol = TG_NET_PROTOCOL_TCP;
		xStreamInfo.xConnectInfo.nTimeOut      = 5000;

		nRet =  TG_Net_StartRealStream(&xStreamInfo,m_hRealStream);
	}

	return nRet;
}
int TransRealStream::StopRealStream()
{
	if( NULL != m_hRealStream )
	{
		TG_Net_StopRealStream( m_hRealStream );
		m_hRealStream = NULL;
	}

	return 0;
}
int TransRealStream::GetItemCount(UINT16 &nCount)
{
	nCount = (UINT16)m_arTransItem.size();

	return 0;
}

int TransRealStream::AddItem( TG_SVR_RS_INFO *pRsInfo )
{
	switch( pRsInfo->nEnable )
	{
	case 1:
		{
			m_arTransItem.push_back( *pRsInfo );
		}
		break;
	case 0x10:
		break;
	case 0x20:
		break;
	case 0:
		{
			UINT16 nCount = (UINT16)m_arTransItem.size();
			for(UINT16 i=0;i<nCount;i++)
			{
				if( pRsInfo->hCameraItem == m_arTransItem[i].hCameraItem )
				{
					m_arTransItem.erase( m_arTransItem.begin()+i );
					break;
				}
			}
		}
		break;
	default:
		break;
	}

	return 0;
}


BOOL TransRealStream::RealDataCallBack(VOID *pBufferData, UINT32 nBufSize, UINT32 nContext, void *pContext)
{
	if( NULL != pContext )
	{
		return ( (TransRealStream*)pContext )->RealStreamData( pBufferData,nBufSize );
	}

	return FALSE;
}

BOOL TransRealStream::RealStreamData(VOID *pBufferData, UINT32 nBufSize)
{
    if( NULL != pBufferData && nBufSize > 0 && NULL != m_hTransCamera )
	{
        TG_SvrNet_SetRealStreamFrame( m_hTransCamera,pBufferData,nBufSize );
	}

	return TRUE;
}

